<?php
/**
 * Created by PhpStorm.
 * User: 何健喔喔喔
 * Date: 2016/12/19
 * Time: 16:00
 */
    function http_curl($url){
        $ch=curl_init();
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLINFO_HEADER_OUT,true);
        $opt=curl_exec($ch);
        curl_close($ch);
        return $opt;
    }
    function get_token(){
        $appid="wxf2c0b4cfd0018d0e";
        $secret="55926fccdebee063eff40a6604f3b088";
        $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
        $json=http_curl($url);
        $json=json_decode($json);
        //var_dump($json);
        return $json->access_token;
    }
    
    //var_dump($result);
    function set_msg()
    {
    
        $token=get_token();
        //var_dump($token);
        $url="https://api.weixin.qq.com/cgi-bin/user/get?access_token={$token}";
        $result=http_curl($url);
        $result=json_decode($result);
        //$result=$result['openid'];
        $result=$result->data->openid;
        //var_dump($result);
        
        
        
        foreach ($result as $id) {
        
            //获取access_token
            //$access_token = getaccess_token();
            $token = get_token();
            echo $id;
            //下面是要填充模板的信息
            /*$formwork = '{
           "touser":"ogVBwwv4N9SBwtOYFlX_6I8iWIjc",
           "template_id":"Eo_MA64WZd14I2Bzm4D2LEJQyj4r4R5MxgOfIVuOe6A",
           "url":"http://www.wangwenxiao.com",            
           "data":{
                   "productType": {
                       "value":"这里是自己定义的标题",
                       "color":"#173177"
                   },
                   "name":{
                       "value":"hejian",
                       "color":"#173177"
                   },
                   "number":{
                       "value":"无数",
                       "color":"#173177"
                   },
                   "expDate":{
                       "value":"5555",
                       "color":"#173177"
                   },
                   "remark": {
                       "value":"这里填写时间",
                       "color":"#173177"
                   }
           }
       }';
    */
    
            $template=array(
                'touser'=>$id,
                'template_id'=>"Eo_MA64WZd14I2Bzm4D2LEJQyj4r4R5MxgOfIVuOe6A",    //模板的id
                'url'=>"http://weixin.qq.com/download",
                'topcolor'=>"#FF0000",
                'data'=>array(
                    'productType'=>array('value'=>"hejian",'color'=>"#00008B"),    //函数传参过来的name
                    'name'=>array('value'=>"hejian",'color'=>'#00008B'),        //函数传参过来的zu
                    'number'=>array('value'=>"hejian",'color'=>'#00008B'),   //时间
                    'expDate'=>array('value'=>"hejian",'color'=>'#00008B'),
                    'remark'=>array('value'=>"hejian",'color'=>'#00008B'),//函数传参过来的ramain
                )
            );
    
            var_dump($template);
            $formwork=json_encode($template);
    
    
    
    
    
            $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$token}";
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $formwork);
            $data = curl_exec($ch);
            curl_close($ch);
            echo "<br>";
            //return $data;
        }
    
    }
    echo set_msg();